var userid=$("#userid").val();
$(document).ready(function(){
	
	$.ajax({
		type:"post",
		url:"../userInf",
		data:{
			user_id:userid,
			password:0,
		},
		success:function(res){
			$("#teacherName").html(res.name);
			$("#teacherId").html(res.id);
			$("#teacherEmail").html(res.email);
		}
	});
	
	$.ajax({
		type:"post",
		url:"../addSubject",
		data:{
			user_id:userid,
			operate:1,
			subjectid:0,
		},
		success:function(res){
			var num = res.count;
			for(var i=1;i<=num;i++){
				
				//console.log(res["id"+i]);
				//console.log(res["name"+i]);
				loadSubject(res["id"+i],res["name"+i]);
				
			}
		}
	});
	
	$.ajax({
		type:"post",
		url:"../addSubject",
		data:{
			user_id:userid,
			operate:2,
			subjectid:0,
		},
		success:function(res){
			var num = res.count;
			for(var i=1;i<=num;i++){
				$("#subjectList").append("<option id=\"option"+res["id"+i]+"\" value=\""+res["id"+i]+"\">"+res["name"+i]+"</option>");

			}
			
		}
	});
	
	
	
})

function loadSubject(id,name){
	$.ajax({
		type:"post",
		url:"htmlPart/subjectCard.html",
		data:{},
		dataType: "html",
		success:function(res){
			$("#subjectPart").append(res);
			
			$("#card").attr("id","card"+id);
			
			$("#subjectName").html(name);
			$("#subjectName").attr("id","subjectName"+id);
			
			$("#subjectImg").attr("src","../img/subject_"+id+".jpg");
			$("#subjectImg").attr("id","subjectImg_"+id);
			
			$("#delete").click(function(){subjectDelete(id);});
			$("#delete").attr("id","subjectDelete"+id);
		}
	});
}


function subjectDelete(id){
	$.ajax({
		type:"post",
		url:"../addSubject",
		data:{
			user_id:userid,
			operate:4,
			subjectid:id,
		},
		success:function(res){
			if(res["delete"]!=0){
				window.location.reload();
			}
			
		}
	});
}



$("#addSubject").click(function(){
	
	if($("#subjectList").val()!=null&&$("#subjectList").val()!=""){
		$.ajax({
			type:"post",
			url:"../addSubject",
			data:{
				user_id:userid,
				operate:3,
				subjectid:$("#subjectList").val(),
			},
			success:function(res){
				if(res.insert!=0){
					window.location.reload();
				}
				
			}
		});
	}else alert("你已添加所有科目");
	
})